﻿using FPSO.Models.Constant;
using SafeLoad.NControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static FPSO.Models.Constant.TankConstant;

namespace FPSO.HMI.Views.Dialog
{
    /// <summary>
    /// WBTTaskDialog.xaml 的交互逻辑
    /// </summary>
	/// NOTICE 现在暂时只进行压载舱倒舱操作
    public partial class WBTSwapTaskDialog : Window
    {
		private Dictionary<string, double> localVolmDic = new Dictionary<string, double>();
		private Dictionary<string, double> localLevelDic = new Dictionary<string, double>();
		//g_Adjustvol记录需调载的压载水体积，缺省使用100方
		public string g_TargetTank = string.Empty;
		public double g_Adjustvol = 100;
		public bool g_HeelChecked = false;
		private bool l_TankType = false;
		public WBTSwapTaskDialog(string tankName,Dictionary<string, double> currentVolmDic, Dictionary<string, double> currentLevelDic,bool tankType)
        {
            InitializeComponent();

			localVolmDic = currentVolmDic;
			localLevelDic = currentLevelDic;

			l_TankType = tankType;

			if (tankType == false)
			{
				DialogTitle.Text = "货油舱调载计划";
				cmbTaskType.Items.Add(new ComboBoxItem() { Content = TaskTypeConstant.SWAP_NAME });
				//如果是货油调载则隐藏横倾调整的选项
				g_HeelChecked = false;
				chbAdjHeelTask.Visibility = Visibility.Hidden;
			}
			else
			{
				DialogTitle.Text = "压载舱调载计划";
				cmbTaskType.Items.Add(new ComboBoxItem() { Content = TaskTypeConstant.SWAPBALLAST_NAME });
			}
			cmbTaskType.SelectedIndex = 0;
			cmbSourceWBT.Text = tankName;

			string tankid = TankConstant.GetTankId(tankName);
			double srctanklevel = localLevelDic[tankid];
			double srctankvol = localVolmDic[tankid];

			List<ConstantTankInfo> taskInfos = TankConstant.getWBTTank();
			if (l_TankType == false)
				taskInfos = TankConstant.getCOTTank();

			foreach (ConstantTankInfo info in taskInfos)
			{
				string tankid0 = info.TankID;
				if ((tankid.IndexOf("_P") > -1 && tankid0.IndexOf("_P") == -1)
					|| (tankid.IndexOf("_S") > -1 && tankid0.IndexOf("_S") == -1))
				{
					double tartanklevel = localLevelDic[tankid0];
					//初始舱液位比目标舱液位高才能实施倒舱
					if(srctanklevel >= tartanklevel)
					{
						ComboBoxItem wbtItem = new ComboBoxItem() { Content = info.TankName, Tag = info };
						cmbTargetWBT.Items.Add(wbtItem);
					}
				}
			}
			txbWBTTargetFill.Text = string.Format("{0:N2}", g_Adjustvol); ;
			cmbTargetWBT.SelectedIndex = 0;
			g_TargetTank = ((ConstantTankInfo)((ComboBoxItem)cmbTargetWBT.SelectedItem).Tag).TankName;
			chbAdjHeelTask.IsChecked = g_HeelChecked;
			SetAdjustVols();
		}
        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            try
            {
				SetAdjustVols();

				if (cmbTargetWBT.SelectedItem == null)
                {
                    throw new Exception("请选择舱室!");
                }
                if (cmbTaskType.SelectedItem == null)
                {
                    throw new Exception("请选择装载类型!");
                }
				if(txbWBTTargetFill.Text == null)
				{
					throw new Exception("请输入舱室调载量!");
				}
				//这里输出g_TargetTank，g_Adjustvol，如果不确认，则清空这两个数据
				this.DialogResult = true;
				this.Close();
			}
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
			g_TargetTank = string.Empty;
			g_Adjustvol = 0;
			this.DialogResult = false;
			this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
		}

		private void WindowMove(object sender, MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
				DragMove();
			}
		}

		private void cmbSourceWBT_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			g_TargetTank = ((ConstantTankInfo)((ComboBoxItem)cmbTargetWBT.SelectedItem).Tag).TankName;
			SetAdjustVols();
		}

		private void chbAdjHeelTask_Checked(object sender, RoutedEventArgs e)
		{
			g_HeelChecked = !g_HeelChecked;
			string SrcTank = cmbSourceWBT.Text;
			List<ConstantTankInfo> taskInfos = TankConstant.getWBTTank();
			if (l_TankType == false)
				taskInfos = TankConstant.getCOTTank();
			foreach (ConstantTankInfo info in taskInfos)
			{
				string tankname = info.TankName;
				//这里需要根据卸载舱室的调载量计算导入舱室是否满足要求
				if (tankname == SrcTank)
				{
					string tankid0 = info.TankID;
					double current_vol = localVolmDic[tankid0];
					g_Adjustvol = current_vol;
					break;
				}
			}
			SetAdjustVols();
		}
		private void txbWBTTargetFill_LostFocus(object sender, RoutedEventArgs e)
		{
			SetAdjustVols();
		}
		private void SetAdjustVols()
		{
			//根据选择的舱室做检查
			g_Adjustvol = Convert.ToDouble(txbWBTTargetFill.Text);
			string SrcTank = cmbSourceWBT.Text;
			//这里根据初始舱可排载量和目标舱可装载量确定调载量g_Adjustvol
			List<ConstantTankInfo> taskInfos = TankConstant.getWBTTank();
			if (l_TankType == false)
				taskInfos = TankConstant.getCOTTank();
			foreach (ConstantTankInfo info in taskInfos)
			{
				string tankname = info.TankName;
				//这里需要根据卸载舱室的调载量计算导入舱室是否满足要求
				//从初始舱可排载量/目标舱可装载量/调载输入量中找到最小的一个
				if (tankname == SrcTank)
				{
					string tankid0 = info.TankID;
					double current_vol = localVolmDic[tankid0];
					txbWBTInitFill.Text = string.Format("{0:N2}", current_vol);
					if (current_vol < g_Adjustvol)
						g_Adjustvol = current_vol;
				}
				else if (tankname == g_TargetTank)
				{
					string tankid0 = info.TankID;
					double current_vol = localVolmDic[tankid0];
					txbTarWBTInitFill.Text = string.Format("{0:N2}", current_vol);
					double total_vol = info.TankVolume;
					double resvol = total_vol - current_vol;
					if (resvol < g_Adjustvol)
					{
						g_Adjustvol = resvol;
					}
				}
			}
			txbWBTTargetFill.Text = string.Format("{0:N2}", g_Adjustvol);
			double tarvol = Convert.ToDouble(txbTarWBTInitFill.Text) + g_Adjustvol;
			txbTarWBTTargetFill.Text = string.Format("{0:N2}", tarvol);
			return;
		}
	}
}
